<?php
$album_id = $_GET['album_id'];
$photo_id = $_GET['id'];
$m = $_GET['m'];
include_once('model/photo.php');
include_once('model/search.php');
$search = new LZ_Search;
$photo = new LZ_Photo;

$image_array = $photo->get_one($photo_id);
if ($image_array['tag'])
{
	$_arr = explode(',',$image_array['tag']);
	foreach($_arr as $_tag)
	{
		$image_array['tags'][] = array('tag'=>$_tag);
	}
}

$view_data['site_keywords'] = $image_array['tag'];
$view_data['site_description'] = $image_array['description'];
$image_array['description'] = str_replace(array("\n","\r"),array("<br />",""),$image_array['description']);
$view_data['image'] = $image_array;
$view_data['title'] = $image_array['title'].' - '.PHOTO_CONTENT;





if ($album_id)
{
	include_once('model/photo_album.php');
	$photo_album = new LZ_Photo_Album;
	$album = $photo_album->get_one($album_id);
	$ids = $album['photo_ids'];
	if ($ids)
	{
		$view_data['album_id'] = $album_id;
		$arr = explode(',',$ids);
		$key = array_search($photo_id,$arr);
		if ($key !== false)
		{
			$pre_key = ($key >= 1)?$key-1:count($arr)-1;
			$_p = $photo->get_one($arr[$pre_key]);
			$view_data['previous_photo'] = $_p;
		
			$next_key = ($key < count($arr)-1)?$key+1:0;
			$_p = $photo->get_one($arr[$next_key]);
			$view_data['next_photo'] = $_p;
		
			$album['total_photos'] = count($arr);
			$album['this_index'] = intval($key)+1;
			$view_data['album'] = $album;
		
			$view_data['related_images'] = array();
			for($i=$key-2;$i<=$key+2;$i++)
			{
				if ($i<0)
					$__key = intval(count($arr)+intval($i)-1);
				else if ($i>=count($arr))
					$__key = intval($i)-count($arr)+1;
				else $__key = $i;
				$view_data['related_images'][] = $photo->get_one($arr[$__key]);
			}
		}
	}
}
else
{
	if ($m == 'related_images')
	{
		$view_data['related_images'] = array();
		$t = $config['related_images']?$config['related_images']:30;
		$ids = $search->search('photo',$image_array['tag'],$t+1,false,true);
		foreach($ids as $id)
		{
			if (count($view_data['related_images']) >= $t) break;
			if ($id != $photo_id ) $view_data['related_images'][] = $photo->get_one($id);
		}
		$temp = template('related_images.html');
		$temp->assign($view_data);
		$temp->output();
		die;
	}
	else
	{
		$view_data['related_images'] = array();
		$ids = $search->search('photo',$image_array['tag'],6,false,true);
		foreach($ids as $id)
		{
			if (count($view_data['related_images']) >= 5) break;
			if ($id != $photo_id ) $view_data['related_images'][] = $photo->get_one($id);
		}
	}
}

$_SESSION['last_list_page'] = 'index.php?p=photo&id='.$image_array['photo_id'];
include_once('left.php');
?>